package page;

import java.util.ArrayList;
import java.util.List;

import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

/**
 * PageParser class parses HTML code for URLs and words.
 * 
 * @version February 6, 2012
 * @author Jay Fernandez
 * @author Kyle Richardson
 */
public class PageParser {
	/**
	 * Parses the web page for all URLs.
	 * 
	 * @param page The web page.
	 * @return list of addresses.
	 */
	public static List<String> parseURLs(Document doc) {
		final List<String> pages = new ArrayList<String>();
		
		Elements links = doc.select("a");
		for (Element link : links) {
			pages.add(link.attr("abs:href"));
		}
		return pages;
	}
	
	/**
	 * Parses the web page for all used words by extracting only the text.
	 * 
	 * @param url The URL of the page.
	 * @return list of words.
	 */
	public static List<String> parseWords(Document doc) {
		final List<String> words = new ArrayList<String>();
		
		String text = doc.body().text();
		text = text.toLowerCase();
		String[] parsed = text.split("[ /\\?,.!@#$%^&*(){}+]+");
		for (int i = 0; i < parsed.length; i++) {
	         words.add(parsed[i]);
		}
		//System.out.println(words.toString()); //TODO Remove after debug
		return words;
	}
}
